package com.unicom.lottery.util;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

public class LotteryManger {
    /**
     * 计算概率
     * @param rates
     * @return 抽中的位置
     */
    public static int getRandomIndex(List<BigDecimal> rates) {
        Random random = new Random();
        BigDecimal ranNum = new BigDecimal(random.nextDouble());
        BigDecimal sumRate = new BigDecimal("0.0");
        for(BigDecimal rate:rates) {
            sumRate = sumRate.add(rate);
        }
        BigDecimal minVal = new BigDecimal("0.0");
        BigDecimal maxVal = new BigDecimal("0.0");
        for (int i = 0; i < rates.size(); i++) {
            BigDecimal rangeVal = rates.get(i).divide(sumRate, BigDecimal.ROUND_HALF_EVEN);
            maxVal = minVal.add(rangeVal);
            boolean exp1 = false;
            if(i == 0) {
                exp1 = (minVal.compareTo(ranNum)<=0);
            } else {
                exp1 = (minVal.compareTo(ranNum)<0);
            }
            if(exp1&&(ranNum.compareTo(maxVal)<=0)) {
                return i;
            }
            minVal = minVal.add(rangeVal);
        }
        throw new RuntimeException("Get Random Index Error");
    }

}
